//
//  MusicTool.m
//  musicPlayer
//
//  Created by 遇水 on 15/6/18.
//  Copyright (c) 2015年 YS. All rights reserved.
//

#import "MusicTool.h"
#import "MusicModel.h"
#import "MJExtension.h"

@implementation MusicTool
static NSArray *_musics;
static MusicModel *_playingMusic;

+(NSArray *)musics
{
    if (!_musics) {
        _musics = [MusicModel objectArrayWithFilename:@"Musics.plist"];
        
    }
    
    return _musics;
}

+(MusicModel *)playingMusic{
    return _playingMusic;
}

+(MusicModel *)nextMusic{
    int nextIndex = 0;
    if (_playingMusic) {
        int playingIndex = [[self musics]indexOfObject:_playingMusic];
        nextIndex = playingIndex+1;
        if (nextIndex >= [self musics].count) {
            nextIndex = 0;
        }
        
    }
    
    return  [self musics][nextIndex];
}

//工具类需要的是别的控制器告诉工具类播放哪一首
+(void)setPlayingMusic:(MusicModel *)playingMusic{
    //如果传入的音频文件是空返回
    //传入的不再音乐组中，返回
    //传入的是当前正在播放的一样，返回
    if(!playingMusic || ![[self musics]containsObject:playingMusic]) return;
    if (_playingMusic == playingMusic) return;
    
    _playingMusic = playingMusic;

}


+(MusicModel *)previousMusic{
    int previousIndex = 0;
    if (_playingMusic) {
        int playingIndex = [[self musics]indexOfObject:_playingMusic];
        previousIndex = playingIndex-1;
        if (previousIndex < 0) {
            previousIndex = [self musics].count - 1;
        }
    }
    
    return [self musics][previousIndex];
}



@end
